package com.jushu.storbox.database.helper;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class SdCardDBOpenHelper extends SQLiteOpenHelper {

	private static int DATABASE_VERSION = 1;
	
	ITableProperties properties = null;
	
	/**
	 *  make a extenal local database from db file
	 * @param context
	 * @param dbFile the real database file like this: /sdcard/metadata.db
	 * @param properties {@link ITableProperties}
	 */
	public SdCardDBOpenHelper(Context context, File dbFile, ITableProperties properties) {
		super(new DatabaseContext(dbFile.getParentFile(), context), dbFile.getName(), null, DATABASE_VERSION);
		this.properties = properties;
	}
	
	/**
	 * make a extenal local database from db file
	 * @param context
	 * @param dbFile dbFile the real database file like this: /sdcard/metadata.db
	 * @param factory {@link CursorFactory}
	 * @param properties {@link ITableProperties}
	 */
	public SdCardDBOpenHelper(Context context, File dbFile, CursorFactory factory, ITableProperties properties) {
		super(new DatabaseContext(dbFile.getParentFile(), context), dbFile.getName(), factory, DATABASE_VERSION);
		this.properties = properties;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		if(properties != null) {
			List<String> commands = properties.getCreateCommand();
			for(String table:commands) {
				db.execSQL(table);
			}
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if(properties != null) {
			List<String> commands = properties.getUpdataCommand();
			for(String table:commands) {
				db.execSQL(table);
			}
		}
	}

}
